import _ from 'lodash';

// 获取填报版本转为数组
export const versionReporting = obj => {
  const arr = [];
  Object.keys(obj).map(v =>
    arr.push({
      key: v,
      value: obj[v],
    }),
  );
  return arr;
};
// 构成全盘表格数据
export const buildValueOliveColumns = list => {
  const dataSource = [];
  const cloneList = _.cloneDeep(list);
  const keys = ['valueEdition', 'afterContentVO', 'contentVO', 'differenceVO'];

  _.forEach(Object.keys(cloneList[0].valueEdition), v => {
    const obj = {};
    _.forEach(keys, key => {
      obj[key] = cloneList[0][key][v];
    });
    dataSource.push(obj);
  });
  return dataSource;
};
// 修改DifferenceVO
export const modifyDifferenceVOData = arr => {
  const obj = {};
  const x = arr;
  x[0].valueEdition = {
    totalValue: '总货值金额',
    quityValue: '权益货值金额',
    houseCommodityValueCount: '房源货值金额',
    houseAvg: '房源均价',
    saleAreaCount: '房源可售面积',
    roomNumCount: '房源套数',
    parkingValueCount: '车位货值金额',
    parkingAvg: '车位均价',
    parkingNumCount: '车位数',
  };

  const keys = Object.keys(x[0].valueEdition);

  _.forEach(keys, key => {
    obj[key] = {
      diff: x[0].differenceVO[`${key}Diff`],
      per: x[0].differenceVO[`${key}Per`],
    };
  });
  x[0].differenceVO = obj;

  return x;
};

export const hasChildren = (list, item) => {
  let result = false;
  list.forEach(value => {
    if (value.pid === item.id) {
      result = true;
    }
  });
  return result;
};

export const buildTree = (list, pid = '0') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (x.pid === pid) {
      if (hasChildren(list, x)) {
        x.children = [];
        x.children = buildTree(list, x.id);
      }
      tree.push(x);
    }
  });
  return tree;
};

export const getExpandedRowKeys = list => {
  const expandedRowKeys = [];
  const arr = _.filter(list, x => x.type !== 'org_product' && x.type !== 'org_group');
  _.forEach(arr, value => {
    expandedRowKeys.push(value.id);
  });
  return expandedRowKeys;
};

// 创建脚本的dom对象实例
export const createDom = () => {
  const theHead = document.getElementsByTagName('head').item(0);
  const myScript = document.createElement('script');
  myScript.src = process.env.oaUrl;
  myScript.type = 'text/javascript';
  myScript.defer = true;
  theHead.appendChild(myScript);
};
